package com.netty.demo10;

import java.nio.ByteBuffer;

/**
 * Created by Lxk on 2020/2/2.
 */
public class NioTest6 {
    //sliceBuffer同原有buffer共享底层数组
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        for(int i = 0;i < 10; i++){
            byteBuffer.put((byte)i);
        }

        byteBuffer.position(2);
        byteBuffer.limit(6);

        ByteBuffer sliceBuffer = byteBuffer.slice();

        for(int i = 0;i < sliceBuffer.capacity(); i++){
            byte b = sliceBuffer.get();
            b *=2;
            sliceBuffer.put(i,b);
        }

        byteBuffer.position(0);
        byteBuffer.limit(byteBuffer.capacity());

        while(byteBuffer.hasRemaining()){
            System.out.println(byteBuffer.get());
        }
    }

}
